package com.hdfs.api;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
//写内容到hdfs文件
public class HDFSWriteDemo {
    public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {

        //1.获取配置类
        Configuration conf = new Configuration();
        //设置hdfs的URI
        URI uri = new URI("hdfs://10.90.100.101:9820");
        //2.获取文件系统   参数1 hdfs的uri  参数2 配置类对象  参数3 hadoop集群的用户（根据自己实际用填写）
        FileSystem fs = FileSystem.get(uri,conf,"hadoop");
        //3.设置要写入hdfs的目标文件路径
        Path dsf = new Path("/data.txt");
        //4.获取输出流
        FSDataOutputStream out = fs.create(dsf,true);
         ///5.输入写内容
        out.writeBytes("Hello HDFS\n");
        //6.关流
        out.close();
    }
}
